﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace guiSdkTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        libCodeGen.SDKTool tool = new libCodeGen.SDKTool();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //open file button
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "*.dll|*.dll";
            if (ofd.ShowDialog() == true)
            {
                var path = System.IO.Path.GetDirectoryName(ofd.FileName);
                var filename = System.IO.Path.GetFileName(ofd.FileName);
                try
                {
                    tool.LoadDLL(path, filename, true);
                }
                catch (Exception err)
                {
                    MessageBox.Show("loaddll error:" + err.Message);
                }
                UpdateTree(this.infoView.Items);
            }

        }


        private void UpdateTree(ItemCollection itemcoll)
        {
            itemcoll.Clear();
            foreach (var dll in tool.AllDlls)
            {
                var item = CreateDLLItem(dll);
                itemcoll.Add(item);
            }
        }
        static TreeViewItem CreateDLLItem(libCodeGen.ConvertDLL dll)
        {
            TreeViewItem item = new TreeViewItem();
            var dllheadstr = "[N]";
            if (dll.TestExport(out string info))
            {
                dllheadstr = "[Y]";
            }
            var cb = new CheckBox() { Content = dllheadstr + dll.monoAssembly.FullName };

            item.Header = cb;
            item.IsExpanded = true;
            item.Tag = dll;
            cb.IsChecked = dll.IsExport;
            cb.Checked += (s, e) =>
              {
                  dll.IsExport = true;
              };
            cb.Unchecked += (s, e) =>
             {
                 dll.IsExport = false;
             };
            {//add refs
                TreeViewItem itemRefs = new TreeViewItem();
                itemRefs.Header = "引用的其它DLL";
                item.Items.Add(itemRefs);
                itemRefs.IsExpanded = false;
                foreach (var _ref in dll.RefDLLs)
                {
                    var headstr = "[N]";
                    if (_ref.dll != null)
                        headstr = "[Y]";
                    headstr += _ref.fullname;
                    TreeViewItem itemrefdll = new TreeViewItem();
                    itemrefdll.Header = headstr;
                    itemrefdll.Tag = itemrefdll;
                    itemRefs.Items.Add(itemrefdll);
                }
            }
            {
                TreeViewItem itemTypes = new TreeViewItem();
                itemTypes.Header = "TYPEs";
                item.Items.Add(itemTypes);
                itemTypes.IsExpanded = true;

                foreach (var type in dll.RefTypes)
                {
                    var subitem = CreateTypeItem(type);
                    itemTypes.Items.Add(subitem);
                }
            }
            return item;
        }
        static TreeViewItem CreateTypeItem(libCodeGen.ConvertType type)
        {
            var headstr = "[N]";
            if (type.TestExport(out string info))
            {
                headstr = "[Y]";
            }

            var cb = new CheckBox() { Content = headstr + type.monoType.FullName };

            TreeViewItem item = new TreeViewItem();
            item.Header = cb;
            cb.IsChecked = type.IsExport;
            cb.Checked += (s, e) =>
            {
                type.IsExport = true;
            };
            cb.Unchecked += (s, e) =>
            {
                type.IsExport = false;
            };
            item.IsExpanded = false;

            if (type.Ctors.Count > 0)
            { //ctors
                TreeViewItem itemctors = new TreeViewItem();
                itemctors.Header = "CTOR";
                item.Items.Add(itemctors);
                foreach (var c in type.Ctors)
                {
                    var subitem = CreateType_CtorItem(c);
                    itemctors.Items.Add(subitem);
                }
            }
            if (type.Methods.Count > 0)
            { //methods
                TreeViewItem itemmethods = new TreeViewItem();
                itemmethods.Header = "METHOD";
                item.Items.Add(itemmethods);
                foreach (var m in type.Methods)
                {
                    var subitem = CreateType_MethodItem(m);
                    itemmethods.Items.Add(subitem);
                }
            }
            if (type.Fields.Count > 0)
            { //fields
                TreeViewItem itemfields = new TreeViewItem();
                itemfields.Header = "FIELD";
                item.Items.Add(itemfields);
                foreach (var f in type.Fields)
                {
                    var subitem = CreateType_FieldItem(f);
                    itemfields.Items.Add(subitem);
                }
            }
            return item;
        }
        static TreeViewItem CreateType_CtorItem(libCodeGen.ConvertCtor ctor)
        {
            var headstr = "[N]";
            if (ctor.TestExport(out string info))
            {
                headstr = "[Y]";
            }

            var cb = new CheckBox() { Content = headstr + ctor.monoCtor.FullName };
            cb.IsChecked = ctor.IsExport;
            cb.Checked += (s, e) =>
            {
                ctor.IsExport = true;
            };
            cb.Unchecked += (s, e) =>
            {
                ctor.IsExport = false;
            };

            TreeViewItem item = new TreeViewItem();
            item.Header = cb;

            return item;
        }
        static TreeViewItem CreateType_MethodItem(libCodeGen.ConvertMethod method)
        {
            var headstr = "[N]";
            if (method.TestExport(out string info))
            {
                headstr = "[Y]";
            }

            var cb = new CheckBox() { Content = headstr + method.monoMethod.FullName };
            cb.IsChecked = method.IsExport;
            cb.Checked += (s, e) =>
            {
                method.IsExport = true;
            };
            cb.Unchecked += (s, e) =>
            {
                method.IsExport = false;
            };

            TreeViewItem item = new TreeViewItem();
            item.Header = cb;

            return item;
        }
        static TreeViewItem CreateType_FieldItem(libCodeGen.ConvertField field)
        {
            var headstr = "[N]";
            if (field.TestExport(out string info))
            {
                headstr = "[Y]";
            }

            var cb = new CheckBox() { Content = headstr + field.monoField.FullName };
            cb.IsChecked = field.IsExport;
            cb.Checked += (s, e) =>
            {
                field.IsExport = true;
            };
            cb.Unchecked += (s, e) =>
            {
                field.IsExport = false;
            };
            TreeViewItem item = new TreeViewItem();
            item.Header = cb;

            return item;
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {//clear all
            tool = new libCodeGen.SDKTool();
            UpdateTree(this.infoView.Items);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {//refersh
            UpdateTree(this.infoView.Items);

        }
    }
}
